# -*-coding:Utf-8 -* 
""" Button Class
"""
import bge, bgl, blf
from .textbox import *
from .color import *
from .image import *
from .font import *

class Button(TextBox):
	
	DefaultHover = {}
	
	def __init__(self, parent,x, y, text, width = None, height = None):
		TextBox.__init__(self, parent,x, y, text, width, height)
		self.hover_modification = Button.DefaultHover.copy()
		self.toggle_hovermod = False
		
	def toggle_hm(self):
		self.toggle_hovermod = self.hover
		for aKey in self.hover_modification.keys():
			if hasattr(self,aKey):
				value = getattr(self,aKey)
				setattr(self,aKey,self.hover_modification[aKey])
				self.hover_modification[aKey] = value
		
	def _draw(self,x,y,alpha):
		if self.hover:
			self.toggle_hm()
		TextBox._draw(self,x,y,alpha)
		if self.hover:
			self.toggle_hm()
	
	
"""
class Button(Box):
	_theme = {"size":14,
			"font_color":Color(0.0,0.0,0.0),
			"shadow":-1,
			"shadow_color":Color(0.0,0.0,0.0),
			"shadow_offset":(0,0),
			"blur":0,
			"text_align":0
	}
	
	def __init__(self, parent=None, text = ""):
		Box.__init__(self, parent)
		self.childs = None
		self.text = text
	
	def getClassTheme(self, theme):
		Box.getClassTheme(self,theme)
		theme.update(Button._theme)
		
	def render(self, cursor):
		if not self.visible:
			self._visibility = False
			return (0,0,0,0)
		zone = list(cursor.zone)
		theme = self.getProcessTheme()
		text = self.text
		font = theme["font"].Id
		blf.size(font, theme["size"], theme["dpi"])
		if theme["shadow"] < 0:
			blf.disable(font,blf.SHADOW)
		else:
			blf.enable(font,blf.SHADOW)
			blf.shadow(font,theme["shadow"],*theme["shadow_color"].all)
			blf.shadow_offset(font, *theme["shadow_offset"])
		dim = blf.dimensions(font, text)
		dim = [round(dim[0]),round(dim[1])]
		print("dim :", dim)
		if type(theme["padding"]) == tuple:
			padding = theme["padding"]
		else:
			padding = (int(theme["padding"]),int(theme["padding"]),int(theme["padding"]),int(theme["padding"]))
		if theme["width"] == None:
			theme["width"] = int(dim[0]+padding[0]+padding[2])
		if theme["height"] == None:
			theme["height"] = int(dim[1]+padding[1]+padding[3])
		ret = self.renderForme(cursor, theme)
		print("Ret button",ret)
		if ret == False:
			cursor.zone = zone
			self._visibility = False
			return (0,0,0,0)
		else:
			self._visibility = True
		position,size,margin = ret[0],ret[1],ret[2]
		if size[1] > zone[3]:
			size[1] = zone[3]
			margin[3] = 0
		elif (size[1]+margin[3]) > zone[3]:
			margin[3] = zone[3]-size[1]
		if size[0] > zone[2]:
			size[0] = zone[2]
			margin[2] = 0
		elif (size[0]+margin[2]) > zone[2]:
			margin[2] = zone[2]-size[0]
		print(cursor.zone,position,size)
		if theme["text_align"] == 0:
			textPosition = [int(cursor.zone[0]), int( cursor.screen[1]-(cursor.zone[1]+(cursor.zone[3]/2)+(dim[1]/2))), theme["font_color"].all]
		elif theme["text_align"] == 1:
			textPosition = [int(cursor.zone[0]+(cursor.zone[2]/2)-(dim[0]/2)), int( cursor.screen[1]-(cursor.zone[1]+(cursor.zone[3]/2)+(dim[1]/2))), theme["font_color"].all]
		elif theme["text_align"] == 2:
			textPosition = [int(cursor.zone[0]+(cursor.zone[2]-dim[0])), int( cursor.screen[1]-(cursor.zone[1]+(cursor.zone[3]/2)+(dim[1]/2))), theme["font_color"].all]
		cursor.zone = zone
		image = 0
		if type(theme["background"]) == Image and theme["background"].loaded:
			image = theme["background"].id
		if type(theme['color']) == Color:
			self._render = [(int(position[0]),int(cursor.screen[1] - (size[1]+position[1])), int(size[0]), int(size[1])),
							theme['color'].all,
							image,
							theme["border"],
							theme['border_color'].all,
							(font,theme["size"], theme["dpi"]),
							text,
							textPosition,
							(theme["shadow"],theme["shadow_color"].all,theme["shadow_offset"]),
							theme["blur"]
							]
		elif int(size[0]) != 0 or int(size[1]) != 0:
			self._render = [(int(position[0]), int(cursor.screen[1] - (size[1]+position[1])), int(size[0]), int(size[1])),
							None,
							image,
							theme["border"],
							theme['border_color'].all,
							(font,theme["size"], theme["dpi"]),
							text,
							textPosition,
							(theme["shadow"],theme["shadow_color"].all,theme["shadow_offset"]),
							theme["blur"]
							]
		else:
			self._render = None
		print("Button : ",self._render)
		return (position[0]-zone[0],position[1]-zone[1], size[0], size[1])

	@classmethod
	def draw(cls, render):
		if render[1] or render[2]:
			if render[1]:
				bgl.glColor4f(*render[1])
			bgl.glBindTexture(bgl.GL_TEXTURE_2D, render[2])
			bgl.glBegin(bgl.GL_QUADS)
			bgl.glTexCoord2f(0.0,0.0)
			bgl.glVertex2i(render[0][0], render[0][1])
			bgl.glTexCoord2f(0.0,1.0)
			bgl.glVertex2i(render[0][0], render[0][1]+render[0][3])
			bgl.glTexCoord2f(1.0,1.0)
			bgl.glVertex2i(render[0][0]+render[0][2], render[0][1]+render[0][3])
			bgl.glTexCoord2f(1.0,0.0)
			bgl.glVertex2i(render[0][0]+render[0][2], render[0][1])
			bgl.glEnd()
		if render[3]:
			bgl.glColor4f(*render[4])
			bgl.glBegin(bgl.GL_LINE_STRIP)
			bgl.glVertex2i(render[0][0], render[0][1])
			bgl.glVertex2i(render[0][0], render[0][1]+render[0][3])
			bgl.glVertex2i(render[0][0]+render[0][2], render[0][1]+render[0][3])
			bgl.glVertex2i(render[0][0]+render[0][2], render[0][1])
			bgl.glEnd()
		if render[6]:
			fo = render[5][0]
			blf.disable(fo,blf.ROTATION)
			blf.size(*render[5])
			if render[8][0] < 0:
				blf.disable(fo,blf.SHADOW)
			else:
				blf.enable(fo,blf.SHADOW)
				blf.shadow(fo,render[8][0],*render[8][1])
				blf.shadow_offset(fo, *render[8][2])
			if render[9]:
				blf.blur(fo,render[9])
			#blf.enable(fo,blf.CLIPPING)
			#blf.clipping(render[0][0],render[0][1],render[0][0]+render[0][2],render[0][1]+render[0][3])
			blf.position(fo,render[7][0],render[7][1],0.0)
			bgl.glColor4f(*render[7][2])
			blf.draw(fo, render[6])
			
		"""		
				
